﻿@using Gx.Model.ViewModel;
@model LKZeroWarehouseContractDto
@{
    ViewBag.Title = "编辑零仓合同";
    Layout = "~/Views/Shared/InfoMaste.cshtml";
}
<script src="~/Js/Global.js?v=20240706"></script>
<script type="text/javascript">
    $(function () {
        InitChargingUnitCombobox();
        InitColdAlgorithmCombobox();
        InitCustomer();
        $('#sbIsByTon').switchbutton({ checked: @Html.Raw(Model.LKZeroWarehouseContract.IsByTon ? "true" : "false") });
        $('#CustomerId').combogrid('setValue', { Id: '@Model.LKZeroWarehouseContract.CustomerId', CustomerName: '@Model.LKZeroWarehouseContract.CustomerName' });
        $('#ChargingUnit').combobox('setValue', @Model.LKZeroWarehouseContract.ChargingUnit);
        $('#ColdAlgorithm').combobox('setValue', @Model.LKZeroWarehouseContract.ColdAlgorithm);
    });

    var alldom = new Array();
    var delcost = new Array();

    function initDom() {
        $("input[type='hidden'][id^='DomId']").each(function () {
            var d = $(this).val();
            if ($.inArray(d, alldom) === -1) {
                // 如果不存在，则添加到数组中
                alldom.push(d);
            }
        });
    }

    function addDom() {
        var domId = $("#DomNum").val();
        $.post("/LKWarehouseContract/_AddLKZeroWarehouseContract", { domId: domId }, function (html) {
            $.messager.progress('close');
            $(".gridtable").append(html);
            $.parser.parse($("#" + domId));
            $("#DomNum").val(Number(domId) + 1);

            initDom();
            var d = Number(domId);
            if ($.inArray(d, alldom) === -1) {
                // 如果不存在，则添加到数组中
                alldom.push(d);
            }

            InitCostTypesCombogrid(domId); (domId);
        });
    }

    function delDom(domId) {
        initDom();
        var deid = $("#CostId" + domId).val();
        delcost.push(deid);
        alldom = $.grep(alldom, function (value) {
            return value != domId;
        });

        $("#" + domId).remove();
    }


    function editLKZeroWarehouseContract() {
        var validate = $("#fm").form("validate")
        if (!validate) {
            return;
        }
        var isByTon = $('#sbIsByTon').switchbutton('options').checked;
        var data = {
            "Id": $("#Id").val(),
            "IsValid": $("#IsValid").val(),
            "ContractNo": $("#ContractNo").textbox('getValue'),
            //"CustomerId": $("#CustomerId").combobox('getValue'),
            //"CustomerName": $("#CustomerName").textbox('getValue'),
            "ContractTime": $("#ContractTime").datebox('getValue'),
            //"BeginTime": $("#BeginTime").datebox('getValue'),
            //"EndTime": $("#EndTime").datebox('getValue'),
            "Tonnage": $("#Tonnage").numberbox('getValue'),
            "ChargingUnit": $("#ChargingUnit").combobox('getValue'),
            "ColdUnitPrice": $("#ColdUnitPrice").numberbox('getValue'),
            "ColdAlgorithm": $("#ColdAlgorithm").combobox('getValue'),
            //"Cycle": $("#Cycle").numberbox('getValue'),
            "Days": $("#Days").numberbox('getValue'),
            "Remark": $("#Remark").val(),
            "IsByTon": isByTon

        };
        //var domNum = $("#DomNum").val();
        initDom();
        var CostList = new Array();
        alldom.forEach(function (value, index) {
            //console.log(index + ': ' + value);
            var i = value;
            var obj = $("#CostType" + i);
            if (obj.length == 0) {
                return false;
            }
            var costTypeId = obj.combogrid('getValue');
            if ($.trim(costTypeId) == "") {
                return false;
            }
            var Id = $("#CostId" + i).val();
            var ContractId = $("#ContractId" + i).val();
            var CostTypeId = $("#CostType" + i).combobox('getValue');
            var CostType = $("#CostType" + i).combobox('getText');
            var Price = $("#Price" + i).numberbox('getValue');
            var Unit = $("#Unit" + i).textbox('getValue');
            var ContractType = $("#ContractType" + i).val();
            var InStorage = $("#sbInStorage" + i).switchbutton('options').checked;
            var OutStorage = $("#sbOutStorage" + i).switchbutton('options').checked;
            var TransferStorage = $("#sbTransferStorage" + i).switchbutton('options').checked;


            //if (CostType == "" || Price == "" || Unit == "") {
            //    Helper.Show("请检查第" + i + "行费用信息");
            //    return;
            //}
            var cost = {
                "Id": Id,
                "ContractId": ContractId,
                "CostTypeId": CostTypeId,
                "ContractType": ContractType,
                "CostType": CostType,
                "Price": Price,
                "Unit": Unit,
                "InStorage": InStorage,
                "OutStorage": OutStorage,
                "TransferStorage": TransferStorage
            };
            CostList.push(cost);
        });

        var ids = $("#deletecostid").val();
        var d = JSON.stringify({ LKZeroWarehouseContract: data });
        $.messager.progress({
            title: '请稍后',
            msg: '数据保存中...',
        });
        window.setTimeout(function () {
            $.ajax({
                url: '/LKWarehouseContract/EditLKZeroWarehouseContract',
                type: "post",
                data: JSON.stringify({ LKZeroWarehouseContract: data, CostList: CostList, DeleteIds: delcost, AttachmentId: $("#hidFileId").val() }),
                contentType: 'application/json; charset=utf-8',
                success: function (json) {
                    $.messager.progress('close');
                    Helper.Show(json.Msg);
                    if (json.Success) {
                        setTimeout(function () { window.location.href = "/LKWarehouseContract/LKZeroWarehouseContractList"; }, 2000);
                    }
                }
            });
        }, 2000);
    }

</script>
<link href="~/Content/css/custome.css" rel="stylesheet" />
<div title="编辑零仓合同" style="width:auto; height:auto; padding-left:5px; font-size:14px;  ">
    <div class="ftitle">
        @*@if (Model.LKZeroWarehouseContract.Status == (int)Gx.Model.Enums.EntryBillStatus.Submitted)
            {*@
        <a href="javascript:" class="easyui-linkbutton" iconCls="icon-ok" style="height:32px" onclick="editLKZeroWarehouseContract()">保存</a>
        @*}*@
        <a href="/LKWarehouseContract/LKZeroWarehouseContractList" class="easyui-linkbutton" iconCls="icon-back" style="height:32px">返回</a>
    </div>
    <form id="fm" method="post" enctype="multipart/form-data" novalidate>
        <input type="hidden" id="Id" value="@Model.LKZeroWarehouseContract.Id" />
        <input type="hidden" id="IsValid" value="@Model.LKZeroWarehouseContract.IsValid.ToString()" />
        <input type="hidden" id="hidFileId" />
        <div class="ftitle">基础信息</div>
        <div class="fitem1">
            <label>
                单据编号:
            </label>
            <input class="easyui-textbox" readonly="readonly" id="ContractNo" name="ContractNo" value="@Model.LKZeroWarehouseContract.ContractNo" style="width: 173px; height: 30px;">
            <label>
                开单日期:
            </label>
            <input class="easyui-datebox" name="ContractTime" id="ContractTime" style="width: 173px; height: 30px; " value="@Model.LKZeroWarehouseContract.ContractTime.ToString("yyyy-MM-dd")" data-options="required:true">
            <label>
                天数:
            </label>
            <input name="Days" id="Days" class="easyui-numberbox" value="@Model.LKZeroWarehouseContract.Days" style="width: 173px; height: 30px;" />
            @*<label>
                    客户编号:
                </label>
                <select class="easyui-combogrid" style="width: 173px; height: 30px;" name="CustomerId" id="CustomerId"></select>
                <label>
                    客户名称:
                </label>
                <input name="CustomerName" id="CustomerName" class="easyui-textbox" style="width: 173px; height: 30px;" value="@Model.LKZeroWarehouseContract.CustomerName" readonly="readonly" />*@
        </div>
        @*<div class="fitem1">
                <label>
                    开单日期:
                </label>
                <input class="easyui-datebox" name="ContractTime" id="ContractTime" style="width: 173px; height: 30px; " value="@Model.LKZeroWarehouseContract.ContractTime.ToString("yyyy-MM-dd")" data-options="required:true">
                <label>
                    开始日期:
                </label>
                <input class="easyui-datebox" name="BeginTime" id="BeginTime" style="width: 173px; height: 30px; " value="@Model.LKZeroWarehouseContract.BeginTime.ToString("yyyy-MM-dd")" data-options="required:true">
                <label>
                    结束日期:
                </label>
                <input class="easyui-datebox" name="EndTime" id="EndTime" style="width: 173px; height: 30px; " value="@Model.LKZeroWarehouseContract.EndTime.ToString("yyyy-MM-dd")" data-options="required:true">
            </div>*@
        <div class="fitem1">
            <label>
                不足n吨算n吨:
            </label>
            <input name="Tonnage" id="Tonnage" class="easyui-numberbox" data-options="required: true" style="width: 173px; height: 30px;" value="@Model.LKZeroWarehouseContract.Tonnage" autocomplete="off" />
            <label>
                计费单位:
            </label>
            <input name="ChargingUnit" id="ChargingUnit" class="easyui-combobox" style="width: 173px; height: 30px;" />
            <label>
                按吨操作:
            </label>
            <input class="easyui-switchbutton" id="sbIsByTon" data-options="onText:'是',offText:'否'">
        </div>
        <div class="fitem1">
            <label>
                冷藏单价:
            </label>
            <input name="ColdUnitPrice" id="ColdUnitPrice" class="easyui-numberbox" value="@Model.LKZeroWarehouseContract.ColdUnitPrice" data-options="required: true,precision:'2'" style="width: 173px; height: 30px;" autocomplete="off" />
            <label>
                冷藏算法
            </label>
            <input name="ColdAlgorithm" id="ColdAlgorithm" class="easyui-combobox" style="width: 173px; height: 30px;" />
            @*<label>
                    周期:
                </label>
                <input name="Cycle" id="Cycle" class="easyui-numberbox" value="@Model.LKZeroWarehouseContract.Cycle" style="width: 173px; height: 30px;" />*@

        </div>
        @*<div class="fitem1">
                <label>
                    图片:
                </label>
                <input id="filebox" class="easyui-filebox" name="file" data-options="buttonText:'选择文件',accept:'image/*',onChange:fileChange" /><a href="@Model.FilePath" target="_blank">@Model.FileName</a>
            </div>*@
        <div class="fitem1">
            <label>
                备注:
            </label>
            <textarea rows="4" cols="4" style="width: 400px; height: 90px; vertical-align: top" name="Remark" id="Remark" autocomplete="off" placeholder="200字以内">@Model.LKZeroWarehouseContract.Remark</textarea>
        </div>
        <div class="ftitle">费用明细</div>
        <div id="Wl">
            <div style="height: 300px; ">
                <input id="DomNum" type="hidden" value="@(Model.CostList.Count+1)" />
                @{
                    int i = 1;
                }
                <a href="javascript:" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="addDom()">添加</a>
                <input type="hidden" id="deletecostid" />
                <table class="gridtable">
                    <thead>
                        <tr>
                            <th>费用类别</th>
                            <th>价格</th>
                            <th>单位</th>
                            <th>在入库单显示</th>
                            <th>在出库单显示</th>
                            <th>在调仓单显示</th>
                            <th>操作</th>
                        </tr>
                        @foreach (var item in Model.CostList)
                        {
                            <tr id="@i">
                                <td>
                                    <input class="easyui-combobox" id="@("CostType")@i" value="@item.CostType" style="width: 120px; height: 30px;" />
                                    <input type="hidden" id="@("DomId")@i" value="@i" />
                                    <input type="hidden" id="@("CostId")@i" value="@item.Id" />
                                    <input type="hidden" id="@("ContractId")@i" value="@item.ContractId" />
                                    <input type="hidden" id="@("ContractType")@i" value="@item.ContractType" />
                                </td>
                                <td><input id="@("Price")@i" class="easyui-numberbox" value="@item.Price" data-options="precision:'2',prompt:'价格'" style="width: 80px; height: 30px;" /></td>
                                <td><input class="easyui-textbox" id="@("Unit")@i" value="@item.Unit" style="width: 120px; height: 30px;"></td>
                                <td><input class="easyui-switchbutton" id="@("sbInStorage")@i" data-options="onText:'是',offText:'否'"></td>
                                <td><input class="easyui-switchbutton" id="@("sbOutStorage")@i" data-options="onText:'是',offText:'否'"></td>
                                <td><input class="easyui-switchbutton" id="@("sbTransferStorage")@i" data-options="onText:'是',offText:'否'"></td>
                                <td><a href="javascript:" class="easyui-linkbutton" iconcls="icon-no" plain="true" style=" width:80px;" onclick="delDom(@i)">删除</a></td>
                            </tr>
                            i++;
                        }
                    </thead>
                </table>
                @{
                    i = 1;
                }
                @foreach (var item in Model.CostList)
                {
                    <script type="text/javascript">
                    $(function () {
                        InitCostTypesCombogrid('@i');
                        $('@("#sbInStorage")@i').switchbutton({ checked: @Html.Raw(item.InStorage ? "true" : "false") });
                        $('@("#sbOutStorage")@i').switchbutton({ checked: @Html.Raw(item.OutStorage ? "true" : "false") });
                        $('@("#sbTransferStorage")@i').switchbutton({ checked: @Html.Raw(item.TransferStorage ? "true" : "false") });
                    });
                    </script>
                    i++;
                }
            </div>
        </div>
        <div class="fitem1">
            @{
                var modifyuser = Model.LKZeroWarehouseContract.ModifyUserId == 0 ? Gx.Dal.Shared.UserContext.CurrentUser.EmpName : Model.LKZeroWarehouseContract.ModifyUser;
                var modifytime = Model.LKZeroWarehouseContract.ModifyTime.ToString("yyyy-MM-dd") == "1911-01-01" ? "" : Model.LKZeroWarehouseContract.ModifyTime.ToString("yyyy-MM-dd");
            }
            <label>
                制单人:
            </label>
            <label>
                @Model.LKZeroWarehouseContract.CreateUser
            </label>

            <label>
                制单日期:
            </label>
            <label>
                @Model.LKZeroWarehouseContract.CreateTime.ToString("yyyy-MM-dd")
            </label>
            <label>
                修改人:
            </label>
            <label>
                @modifyuser
            </label>
            <label>
                修改日期:
            </label>
            <label>
                @modifytime
            </label>
        </div>
        @*<script type="text/javascript">
                    $(function () {
                        $('#CreateTime').datebox('setValue', '@Model.LKZeroWarehouseContract.CreateTime.ToString("yyyy-MM-dd")');
                        $('#ModifyTime').datebox('setValue', '@Model.LKZeroWarehouseContract.ModifyTime.ToString("yyyy-MM-dd")'=='1911-01-01'?'':'@Model.LKZeroWarehouseContract.ModifyTime.ToString("yyyy-MM-dd")');
                        $('#CreateUser').combogrid('setValue', { EmployeeId: '@Model.LKZeroWarehouseContract.CreateUserId', EmpName: '@Model.LKZeroWarehouseContract.CreateUser' });
                        $('#ModifyUser').combogrid('setValue', { EmployeeId: @Model.LKZeroWarehouseContract.ModifyUserId==0?@Gx.Common.LoginHelper.GetUserId():@Model.LKZeroWarehouseContract.ModifyUserId });
                    });
            </script>*@
    </form>
</div>





